package com.zyc.fleamarket.config;

/**
 * @author zed
 * @date 2022/6/16 9:47
 * 统一响应的实体类
 */
public class Response<T> {

    private static final Integer SUCCESS = 200;
    private static final Integer FAIL = -1;

    private Integer code;
    private String msg;
    private T data;

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    private Response(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Response<T> success(Integer code, String msg, T data){
        return new Response<>(code,msg,data);
    }

    public static <T> Response<T> success(T data){
        return success(SUCCESS,"请求成功",data);
    }

    public static <T> Response<T> success(){
        return success(SUCCESS,"请求成功",null);
    }

    public static <T> Response<T> fail(Integer code, String msg, T data){
        return new Response<>(code,msg,data);
    }

    public static <T> Response<T> fail(T data){
        return fail(FAIL,"请求失败",data);
    }

    public static <T> Response<T> fail(){
        return fail(FAIL,"请求失败",null);
    }
}
